//
//  ZJKOriginalStatusView.swift
//  MicroBlog
//
//  Created by VAD on 16/4/20.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit
import SnapKit

class ZJKOriginalStatusView: UIView {
    
    // 当前控件的底部约束
    var bottonCons: Constraint?

    var statusViewModel:ZJKStatusViewModel?{
        didSet{
            //设置头像
            headImage.sd_setImageWithURL(NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            
            // 设置昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            
            // 设置会员图标
            memberImageView.image = statusViewModel?.memberImage
            
            // TODO : 时间与来源 (会重新设置)
            timeLabel.text = statusViewModel?.status?.created_at
            
            sourceLabel.text = statusViewModel?.sourceText
            
            // 设置微博正文内容
            contentLabel.text = statusViewModel?.status?.text
            
            // 0. 卸载之前的底部约束
            bottonCons?.uninstall()
            
            //设置配图数据
            
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                
                //设置数据
                pictureView.dataInfo = (pic_urls, statusViewModel!.originalPictureViewSize)
                
                pictureView.hidden = false
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottonCons = make.bottom.equalTo(pictureView).offset(ZJKStatusCellMargin).constraint
                })
            }else{
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottonCons = make.bottom.equalTo(contentLabel).offset(ZJKStatusCellMargin).constraint
                })
            }
            
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 250/255, alpha: 1)
        //添加控件
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        // 头像
        headImage.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(ZJKStatusCellMargin)
            make.size.equalTo(CGSize(width: ZJKStatusCellHeadImgWH, height: ZJKStatusCellHeadImgWH))
        }
        
        // 约束昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImage)
            make.leading.equalTo(headImage.snp_trailing).offset(ZJKStatusCellMargin)
        }
        // 会员
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(ZJKStatusCellMargin)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImage)
            make.leading.equalTo(nameLabel)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(ZJKStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_trailing).offset(-2)
            make.centerY.equalTo(headImage.snp_bottom).offset(-2)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(ZJKStatusCellMargin)
        }
        //配图视图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(ZJKStatusCellMargin)
            //测试代码
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            bottonCons = make.bottom.equalTo(pictureView.snp_bottom).offset(ZJKStatusCellMargin).constraint
        }

    }
    
    //头像
    private lazy var headImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    //昵称label
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZJKStatusCellNameFontSize)
        label.text = "哈哈"
        return label
    }()
    //会员标识
    private lazy var memberImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership") )

    //时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: ZJKStatusCellTimeFontSize)
        label.text = "刚刚"
        return label
    }()
    //来源
    private lazy var sourceLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZJKStatusCellTimeFontSize)
        label.text = "来自传智播客"
        return label
    }()
    //认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZJKStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * ZJKStatusCellMargin)
        return label
        
    }()
    
    //配图视图
    private lazy var pictureView: ZJKStatusPictureView = ZJKStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
}
